///
///  AutoBiography- V1.0
///  Developer: Team AutoLytics
///  Created: 03-10-11
///  Last Updated: 03-15-11
///

import java.util.*;
import java.io.*;
import java.sql.*;

public class AutoBiography{
  
  /** DB Variables- These variables are used to set up a connection with and communicate with our system's database. This data base holds tables for User Profiles, 
      Car Profiles, and Vehicle Statistics. **/
  static Connection dbConnection = null;
  static String connectString = "jdbc:mysql://ec2-184-73-238-96.compute-1.amazonaws.com:3008/AutoBiography";
  static String driver = "com.mysql.jdbc.Driver";
  static String userid = "Team_AutoLytics";
  static String pwd = "COE1186";
  static Statement stmnt = null;
  static ResultSet r = null;
  static User currUser;
  static ArrayList<User> users = new ArrayList<User>();
  public static ClientModel client;

  public AutoBiography(){
      //Establishing a connection with our online database.
      System.out.println("Starting...");
    try{
      Class.forName(driver).newInstance();
      dbConnection = DriverManager.getConnection(connectString, userid, pwd);
      stmnt=dbConnection.createStatement();
      System.out.println("Connection successful");
    }catch(Exception e){
      System.out.println("Connection Error.");
      System.out.println(e);
      System.exit(0);
    }
    System.out.println("Connected");




    //Try to create our tables. If they already exist, the exception will be caught and no harm will be done.
    try{
      //User Table
        SecureLogin sl =new SecureLogin();
        sl.createTable(dbConnection);   
    }catch(SQLException e0){
      System.out.println("Create User Table Error!");
      System.out.println(e0);
    }


  //  try{
      //Car Table
      // 4-14-11: Evan added CH_ID variable to hold a clearinghouse operator ID.
       Response answer = this.client.getResponse(new Request(2, null, null));
       answer = this.client.getResponse(new Request(3, null, null));


     
     answer = this.client.getResponse(new Request(5, null, null));
    new LoginScreen(dbConnection, client);
  }
  
  public static void setUser(User c){
    currUser = c;
  }
  
  public static User getUser(String un, String pw){
    for(int i=0; i<users.size(); i++){
      if(users.get(i).username.equals(un)){
        if(users.get(i).password.equals(pw)){
          return users.get(i);
        }
      }
    }
    return null;
  }
    public static void addUser(User newguy){
        users.add(newguy);
    }
    public static void removeUser(User todie){
        users.remove(todie);
    }
  public static void main(String[] args) throws java.sql.SQLException{
     client = new ClientModel();
    new AutoBiography();
  }
} 
